package com.example.task1.ui.AllService.movies;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.MoniesFilmBean;
import com.example.task1.bean.MoviesHallBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import retrofit2.Call;

public class MoviesCinemaDetailsActivity extends AppCompatActivity {
    private RecyclerView rvHall;
    private int cinemaId;
    private int movieId;
    private MoviesHallAdapter moviesHallAdapter;
    private TextView end;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movies_cinema_details);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("购票");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        cinemaId = getIntent().getIntExtra("id", 0);
        movieId = getIntent().getIntExtra("ids", 0);
        initView();
        initData();
        setAdapter();
    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN
                , "/prod-api/api/movie/theatre/times/list?theaterId=" + cinemaId + "&movieId=" + movieId);
        Api.send(apiRespCall, MoviesHallBean.class, new CallBack<MoviesHallBean>() {
            @Override
            public void success(MoviesHallBean data) {
                if (data.getRows().size() == 0) {
                    end.setVisibility(View.VISIBLE);
                }else {
                    end.setVisibility(View.GONE);
                    moviesHallAdapter.setData(data.getRows(), cinemaId, movieId);
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void setAdapter() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        moviesHallAdapter = new MoviesHallAdapter(this);
        rvHall.setAdapter(moviesHallAdapter);
        rvHall.setLayoutManager(layoutManager);
    }

    private void initView() {
        rvHall = (RecyclerView) findViewById(R.id.rv_hall);
        end = (TextView) findViewById(R.id.end);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


}